/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.banner.transform;

import ohos.agp.components.Component;

/**
 * 侧滑缩放动画
 *
 * @since 2021-05-06
 */
public class ZoomOutSlideTransformer implements PageTransformer {
    private static final float SCALE_1 = -1;
    private static final float SCALE_2 = 2.0F;
    private static final float SCALE_0_88 = 0.88F;
    private static final float SCALE_0_9 = 0.9F;
    private static final float SCALE_0_14999999 = 0.14999999F;

    @Override
    public void transformPage(Component view, float position) {
        if (position >= SCALE_1 || position <= (float) 1) {
            float height = (float) view.getHeight();
            float width = (float) view.getWidth();
            float scaleFactor = Math.max(SCALE_0_88, (float) 1 - Math.abs(position));
            float vertMargin = height * ((float) 1 - scaleFactor) / SCALE_2;
            float horzMargin = width * ((float) 1 - scaleFactor) / SCALE_2;
            if (position < (float) 0) {
                view.setTranslationX(horzMargin - vertMargin / SCALE_2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / SCALE_2);
            }
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);
            view.setAlpha(SCALE_0_9 + (scaleFactor - SCALE_0_88) / SCALE_0_14999999 * SCALE_0_9);
        }
    }
}